package cn.sdormitory.controller;

import cn.sdormitory.basedata.entity.BDormitory;
import cn.sdormitory.basedata.vo.BDormitoryVO;
import cn.sdormitory.basedata.vo.BStudentAppInfoVo;
import cn.sdormitory.basedata.vo.BStudentAppVo;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.dto.ResultMsg;
import cn.sdormitory.mq.RabbitSenderService;
import cn.sdormitory.service.AppManualAttenceService;
import cn.sdormitory.service.AppStudentService;
import cn.sdormitory.smartdor.entity.AdManualAttence;
import cn.sdormitory.smartdor.entity.SdAttence;
import cn.sdormitory.smartdor.vo.DormitoryAttenceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @创建人：zhouyang
 * @创建时间：2021/10/27 14:19
 * @version：V1.0
 */
@RestController
@RequestMapping("/app/ManualAttence")
@Api("APP端考勤")
public class AppManualAttenceController {
    @Autowired
    private AppManualAttenceService appManualAttenceService;

    @Autowired
    private AppStudentService appStudentService;




    @ApiOperation("listDorManualAttence => APP端查询宿舍考勤信息列表")
    @GetMapping(value = "/listDorManualAttence")
    public ResultMsg listDorAttence(String className, String storey, String accessDate,String buildingNo){
        List<DormitoryAttenceVo> dormitoryAttenceVoList=appManualAttenceService.getAppDorAttenceManual(className,storey,accessDate,buildingNo);
        return ResultMsg.BY_SUCCESS("获取成功",dormitoryAttenceVoList);
    }

    @ApiOperation("getAppDormitoryList => APP端查询宿舍信息列表")
    @GetMapping(value = "/getAppDormitoryList")
    public ResultMsg listDorAttence(){
        List<BDormitoryVO> dormitoryAttenceVoList=appManualAttenceService.getAppDormitoryList();
        return ResultMsg.BY_SUCCESS("获取成功",dormitoryAttenceVoList);
    }

    @ApiOperation("getAppDorListByStorey => APP端根据栋号和楼层查询宿舍信息列表")
    @GetMapping(value = "/getAppDorListByStorey")
    public ResultMsg getAppDorListByStorey(String buildingNo,String storey){
        List<BDormitoryVO> dormitoryAttenceVoList=appManualAttenceService.getAppDorListByStorey(buildingNo,storey);
        return ResultMsg.BY_SUCCESS("获取成功",dormitoryAttenceVoList);
    }

    @ApiOperation("getAppStudentListByDId => APP端查询某宿舍对应学员信息列表")
    @GetMapping(value = "/getAppStudentListByDId")
    public ResultMsg listDorAttence(Long dormitoryId,String accessDate){
        int count=appStudentService.getAppBStudentCount(dormitoryId,accessDate);
        List<BStudentAppVo> studentVoList=new ArrayList<>();
        if(count>0){
            studentVoList=appStudentService.getAppBStudentByDormitoryId(dormitoryId,accessDate);
        }else{
            studentVoList=appStudentService.getAppBStudentByDormitoryIdNo(dormitoryId,accessDate);
        }
        return ResultMsg.BY_SUCCESS("获取成功",studentVoList);
    }

    @ApiOperation("getAppStuInfoById => APP端查询某学员详细信息及当天考勤状态")
    @GetMapping(value = "/getAppStuInfoById")
    public ResultMsg getAppStuInfoById(String studentNo,String accessDate){
        int count=appStudentService.getAppBStudentCountById(studentNo,accessDate);
        List<BStudentAppInfoVo> studentVoList=new ArrayList<>();
        if(count>0){
            studentVoList=appStudentService.getAppStuInfoById(studentNo,accessDate);
        }else{
            studentVoList=appStudentService.getAppStuInfoByIdNo(studentNo,accessDate);
        }
        return ResultMsg.BY_SUCCESS("获取成功",studentVoList);
    }

    @ApiOperation("create => APP端新增考勤信息")
    @PostMapping(value = "/createAppAttence")
    public CommonResult<Integer> create(@RequestBody AdManualAttence adManualAttence) throws IOException {
        int count = appManualAttenceService.addManualAttence(adManualAttence);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
        //System.out.println("attenceDate :" + attenceDate + "\t studentIdArray: " + studentIdArray + "\t statusArray :" + statusArray);
    }

    @ApiOperation("getAppDorListByDorNo => APP端根据栋号+楼层+宿舍号获取宿舍ID")
    @GetMapping(value = "/getAppDorListByDorNo")
    public ResultMsg getAppDorListByDorNo(String buildingNo,String storey,String dormitoryNo){
        Long result=appManualAttenceService.getAppDorListByDorNo(buildingNo,storey,dormitoryNo);
        return ResultMsg.BY_SUCCESS("获取成功",result);
    }

//    @ApiOperation("getSdAttenceByCondition => 根据条件获取宿舍考勤信息")
//    @PostMapping(value = "/getSdAttenceByCondition")
//    public CommonResult getSdAttenceByCondition(@RequestBody SdAttence condition){
//        List<SdAttence> list = appManualAttenceService.getSdAttenceByCondition(condition);
//        return CommonResult.success(list);
//    }

    @ApiOperation("getSdAttenceExcList => 获取宿舍考勤异常数据列表")
    @PostMapping(value = "/getSdAttenceExcList")
    public CommonResult getSdAttenceExcList(@RequestBody SdAttence query){
        List<SdAttence> list = appManualAttenceService.getSdAttenceExcList(query);
        return CommonResult.success(list);
    }

    @ApiOperation("更新考勤状态")
    @PostMapping(value = "/updateStudentSdAttence")
    public CommonResult updateStudentSdAttence(@RequestBody SdAttence entity){
        Date date = new Date();
        entity.setModifyTime(date);
        int num = appManualAttenceService.updateStudentSdAttence(entity);
        if(num >= 1){
            return CommonResult.success("成功");
        }else {
            return CommonResult.failed("失败");
        }
    }

    @ApiOperation("推送同步考勤消息")
    @PostMapping(value = "/publishSyncSdAttence")
    public CommonResult publishSyncSdAttence(@RequestBody SdAttence entity){



        int num = appManualAttenceService.publishSyncSdAttence(entity);
        if(num >= 1){
            return CommonResult.success("成功");
        }
        if(num == -1){
            return CommonResult.failed("宿舍未绑定考勤机");
        }
        return CommonResult.failed("失败");

    }

    @ApiOperation("获取宿舍考勤补卡学生")
    @PostMapping(value = "/getMendCardStuList")
    public CommonResult getMendCardStuList(@RequestBody SdAttence query){
        List<SdAttence> list = appManualAttenceService.getMendCardStuList(query);
        return CommonResult.success(list);
    }

}
